package com.xing.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBService extends SQLiteOpenHelper{
	
	public  static String dataBaseName = "myapp.db";
	public static int currentVersion = 1;
	/**
	 * 问卷基本信息表
	 */
	public static final String HsdModelSurvey ="hsd_model_survey";
	 /**
     * 
     */
	public static final String HsdDataResearch = "hsd_data_research";
	/**
	 *问卷问题模版信息表
	 */
	public static final String questionnaire_question_model_info = "questionnaire_question_model_info";
	/**
	 * 问卷答案模型信息表
	 */
	public static final String questionnaire_answer_model_info = "question_answer_model_info";
	/**
	 * 问卷答案信息表
	 */
	public static final String questionnaire_result_info = "questionnaire_result_model";
	
	public static final String HSD_ACCOUNT = "hsd_account";

	public DBService(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String createHsdAccountSql = "create table "+ DBService.HSD_ACCOUNT +"("+
	        	"_id integer primary key autoincrement,"+
				"username text,"+
	        	"password text,"+
				"loginid text,"+
				"token text)";
	        	
	      db.execSQL(createHsdAccountSql);	
		
		 //创建数据库的同时创建表:
		String sql="create table "+ DBService.HsdModelSurvey+"(" +
				"_id integer primary key autoincrement,"+
				"surveyuuid  text,"+
				"name text,"+
				"subname text,"+
				"description text,"+
				"descriptiontext text,"+
				"createtype integer,"+
				"type integer,"+
				"logo text,"+
				"progressbar integer,"+
				"pagecount integer,"+
				"questioncount integer,"+
				"timeschedule integer,"+
				"starttime text,"+
				"endtime text,"+
				"createtime text,"+
				"creatorname text,"+
				"creatoraccount text,"+
				"firstsavetime text,"+
				"modifytime integer," +
				"status integer)";

        db.execSQL(sql);
        
        /**
         * 调查二级页面列表　　问卷调查信息表
         *  
         * status  1:已完成 2:未完成
         * has_uploaded 1: 已上传  2:未上传  
         *
         */
        String sqlCreateQuestionDetail = "create table "+DBService.HsdDataResearch+"("+
        		"_id integer primary key autoincrement ,"+
        		"researchuuid text,"+
        		"surveyuuid text,"+
        		"name text,"+
        		"sex integer,"+
        		"age integer,"+
        		"telephone text,"+
        		"mobilephone text,"+
        		"email text,"+
        		"documenttype integer,"+
        		"idnumber text,"+
        		"researchaccount text,"+
        		"researchname text,"+
        		"status integr,"+
        		"createtime text,"+
        		"modifytime text,"+
        		"has_uploaded integer)";
        		
        
        db.execSQL(sqlCreateQuestionDetail);

  
     
        String sqlCreateQuestionModel = "create table questionnaire_question_model_info("+
        "id integer primary key,"+
        "questionuuid text,"+
        "surveyuuid text,"+
        "pageuuid text, "+
        "sortindex integer,"+
        "type integer,"+
        "typeid integer,"+
        "description text,"+
        "descriptionview text,"+
        "isgridchildquestion integer,"+
        "fatherquestionuuid String,"+
        "format integer,"+
        "required integer,"+
        "imageurl text,"+
        "instruction text,"+
        "imagetype integer,"+
        "imagewidth integer,"+
        "imageheight integer,"+
        "showpoint integer,"+
        "showhasother integer,"+
        "rangeformat integer,"+
        "rangemax integer,"+
        "rangemin integer,"+
        "createtime text,"+
        "modifytime text)";
        db.execSQL(sqlCreateQuestionModel);
        
        /**
         * 这个后期改一下名字,不是模型表,是结果表
         */
        String createQuestion_answer_model_info = "create table question_answer_model_info("+
        "id integer primary key ,"+
        "answeruuid text,"+
        "surveyuuid text,"+
        "questionuuid text,"+
        "sortindex integer,"+
        "name text,"+
        "hasother integer,"+
        "point integer,"+
        "imagetype integer,"+
        "imageurl text,"+
        "imageheight integer,"+
        "imagewidth integer,"+
        "createtime text)";
        db.execSQL(createQuestion_answer_model_info);
        

        String createQuestoinnaire_result_model_info = "create table questionnaire_result_model("+
        "id integer primary key,"+
        "resultuuid text,"+
        "researchuuid text,"+
        "surveyuuid text,"+
        "fatherquestionuuid text,"+
        "questionuuid text,"+
        "answeruuid text,"+
        "answertext text,"+
        "createtime text,"+
        "modifytime text)";
        db.execSQL(createQuestoinnaire_result_model_info);
        
	
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		//数据库版本发生变化时会调用这个回调函数：
				String sql = "drop table if exists "+DBService.HsdModelSurvey;	
				db.execSQL(sql);
				this.onCreate(db);
	}

}
